﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Web;

namespace KBMVC.Core.Data
{
    class Article
    {

        #region Load Method

        public static KBMVC.Core.Article Load(string ID)
        {
            string XmlFileLocation = @"C:\Users\Trey\Documents\Development\KBMVC\KBMVC\DataFiles\Articles.xml";

            if (ID.Trim().Length <= 0)
            {
                return null;
            }

            XElement doc = XElement.Load(XmlFileLocation);

            var findArticles = from f in doc.Descendants("Article")
                               where f.Attribute("ID").Value == ID
                               select new
                               {
                                   Title = f.Element("Title").Value,
                                   Slug = f.Element("Slug").Value,
                                   Author = f.Element("Author").Value,
                                   Tags = f.Element("Tags").Value,
                                   CreationDate = f.Element("CreationDate").Value,
                                   Content = f.Element("Content").Value
                               };

            foreach (var specificArticle in findArticles)
            {
                KBMVC.Core.Article article =
                    new KBMVC.Core.Article();

                article.Title = specificArticle.Title;
                article.Slug = specificArticle.Slug;
                article.Author = specificArticle.Author;
                article.Tags = specificArticle.Tags;
                article.CreationDate = Convert.ToDateTime(specificArticle.CreationDate);
                article.Content = specificArticle.Content;

                return article;
            }

            return null;
        }

        #endregion

        #region Save Method

        public static bool Save(KBMVC.Core.Article article)
        {
            string XmlFileLocation = @"C:\Users\Trey Gourley\Documents\Development\kbmvc\KBMVC\DataFiles\Articles.xml";

            if (article.Slug.Trim().Length <= 0)
            {
                return false;
            }

            XElement doc = XElement.Load(XmlFileLocation);

            XElement newArticle =
                new XElement("Article", new XAttribute("ID", article.Slug),
                    new XElement("Title", article.Title),
                    new XElement("Slug", article.Slug),
                    new XElement("Author", article.Author),
                    new XElement("CreationDate", article.CreationDate),
                    new XElement("Tags", article.Tags),
                    new XElement("Content", article.Content)
                    );

            doc.AddFirst(newArticle);

            doc.Save(XmlFileLocation);

            return true;
        }

        #endregion

        #region Delete Method

        public static bool Delete(KBMVC.Core.Article article)
        {
            string XmlFileLocation = @"C:\Users\Trey Gourley\Documents\Development\kbmvc\KBMVC\DataFiles\Articles.xml";
          
            if (article.Slug.Trim().Length <= 0)
            {
                return false;
            }

            //TO BE IMPLEMENTED

            return true;
        }

        #endregion

    }
}
